<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="问题处理方案">
          <el-input
            v-model="searchParam.exceptionSolvingScheme"
            placeholder="请输入问题处理方案"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableStop()"
          >批量停用</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          :selectable="selected"
          width="55"
          align="center"
        >
        </el-table-column>
        <el-table-column type="expand">
          <template #default="props">
            <el-table
              :data="props.row.details"
              :border="childBorder"
              style="padding: 10px 0 10px 0"
            >
              <el-table-column
                label="序号"
                type="index"
                width="100"
                align="center"
                show-overflow-tooltip
              ></el-table-column>
              <el-table-column label="问题处理工单" prop="exceptionSolvingWork">
                <template #default="scope">
                  {{
                    dictionaryStore.filterDictName(
                      "problemTicket",
                      scope.row.exceptionSolvingWork
                    )
                  }}
                </template>
              </el-table-column>
              <!-- <el-table-column label="State" prop="state" />
              <el-table-column label="City" prop="city" />
              <el-table-column label="Address" prop="address" />
              <el-table-column label="Zip" prop="zip" /> -->
            </el-table>
          </template>
        </el-table-column>
        <el-table-column
          label="序号"
          type="index"
          width="100"
          align="center"
          show-overflow-tooltip
        ></el-table-column>

        <el-table-column
          label="问题处理方案"
          prop="exceptionSolvingScheme"
          min-width="100"
          show-overflow-tooltip
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="问题类型"
          prop="exceptionSolvingType"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{
              dictionaryStore.filterDictName(
                "ApproachType",
                scope.row.exceptionSolvingType
              )
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="是否启用"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">启用</el-tag>
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              style="color: red"
              :disabled="scope.row.enabled == 0"
              @click="handleTableStop(scope.row.id)"
              >停用</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <qmsexceptionsolvingsetting-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "QMSQMSEXCEPTIONSOLVINGSETTING",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/qms/qmsexceptionsolvingsetting/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  exceptionSolvingScheme: "",
  // remarks: "",
  // createTimeSearch: [],
  // createTimeStart: "",
  // createTimeEnd: "",
  // lastUpdateTimeSearch: [],
  // lastUpdateTimeStart: "",
  // lastUpdateTimeEnd: "",
  // enabled: "",
  // deleted: "",
};

const deleteParam = {
  api: "/qms/qmsexceptionsolvingsetting/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/qms/qmsexceptionsolvingsetting/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开问题处理配置表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改问题处理配置";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增问题处理配置";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开问题处理配置表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "问题处理配置详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0];
    searchParam.value.createTimeEnd = data[1];
  } else {
    searchParam.value.createTimeStart = "";
    searchParam.value.createTimeEnd = "";
  }
}
function getlastUpdateTime(data: any) {
  if (data) {
    searchParam.value.lastUpdateTimeStart = data[0];
    searchParam.value.lastUpdateTimeEnd = data[1];
  } else {
    searchParam.value.lastUpdateTimeStart = "";
    searchParam.value.lastUpdateTimeEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除该数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/qms/qmsexceptionsolvingsetting/deleteItem/${id}`,
      method: "post",
    }).then((res) => {
      ElMessage.success("删除成功！");
      search();
    });
  });
}

async function handleTableStop(id: any) {
  if (id) {
    ElMessageBox.confirm("确认停用该数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      await request({
        url: `/qms/qmsexceptionsolvingsetting/stopUsing`,
        method: "post",
        data: { ids: [id] },
      }).then((res) => {
        ElMessage.success("停用成功！");
        search();
      });
    });
  } else {
    ElMessageBox.confirm("确认停用已选中的数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      await request({
        url: `/qms/qmsexceptionsolvingsetting/stopUsing`,
        method: "post",
        data: { ids: ids.value },
      }).then((res) => {
        ElMessage.success("停用成功！");
        search();
      });
    });
  }
}
// 停用过滤
const selected = (row) => {
  if (row.enabled == 0) {
    return false; //不可勾选
  } else {
    return true; //可勾选
  }
};

const childBorder = ref(false);
</script>
